<template>
  <div class="goods_list">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary" @click="addCategory">添加分类</el-button>
      <tree-table
        show-index
        index-text='#'
        border
        :selection-type='false'
        :expand-type='false'
        :show-row-hover="false"
        :data="goods_list"
        :columns="treetable_colums"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.cat_level=='0'">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level=='1'">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" class="el-icon-edit goods_button" @click="editCate(scope.row)">编辑</el-button>
          <el-button type="danger" class="el-icon-delete goods_button" @click="delCate(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加商品分类的对话框 -->
    <el-dialog
        title="添加商品分类"
        :visible.sync="addCateDialogVisible"
        width="50%"
        @close='closeAddCate'
      >
      <el-form ref="add_cate_form" :model="addCateForm" label-width="100px" :rules="addCateRules">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name" placeholder="请输入分类名"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            expand-trigger="hover"
            :options="addCateCascader"
            :props="addCateCascaderOptions"
            v-model="selectedIds"
            clearable
            >
          </el-cascader>
        </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmAddCate">确 定</el-button>
        </span>
    </el-dialog>

    <!-- 编辑商品分类的对话框 -->
    <el-dialog
      title="编辑商品分类"
      :visible.sync="editCateDialogVisible"
      width="50%">
      <el-form ref="editcate_ref" :rules="editcate_rules" :model="editcate" label-width="100px">
        <el-form-item label="分类名称:" prop="editcate_newname">
          <el-input v-model="editcate.editcate_newname"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEditCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default{
  data(){
    return {
      goods_list:[],
      pagesize:5,
      pagenum:1,
      total:0,
      treetable_colums:[
        {
          label:'分类名称',
          prop:'cat_name'
        },
        {
          label:'是否有效',
          type:'template',
          template:'isok'
        },
        {
          label:'分类登记',
          type:'template',
          template:'order'
        },
        {
          label:'操作',
          type:'template',
          template:'operation'
        }
      ],
      addCateDialogVisible:false,
      addCateForm:{
        cat_pid:0,
        cat_name:'',
        cat_level:0
      },
      addCateRules:{
        cat_name:[
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      addCateCascader:[],//所有商品分类的数组
      addCateCascaderOptions:{//级联选择器的配置对象
        value:'cat_id',
        label:'cat_name',
        children:'children',
        checkStrictly: true 
      },
      selectedIds:[] ,//添加分类中被选中的ID的数组
      editcate:{
        cat_id:'',
        editcate_newname:'',//编辑分类名称的input值
      },
      editCateDialogVisible:false,
      editcate_rules:{
        editcate_newname: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
      }
    }
  },
  created(){
    this.initGoodsList()
  },
  methods:{
    async initGoodsList(){
      const res=await this.$http.get('categories',{params:{
        pagenum:this.pagenum,pagesize:this.pagesize
      }})  
      // console.log(res);
      if(res.data.meta.status!==200) this.$message.error('获取数据失败')
      this.goods_list=res.data.data.result
      this.total=res.data.data.total
    },
    handleSizeChange(newVal){
      this.pagesize=newVal
      this.initGoodsList()
    },
    handleCurrentChange(newVal){
      this.pagenum=newVal
      this.initGoodsList()
    },
    async addCategory(){
      const res=await this.$http.get('categories',{params:{
        type:2
      }})
      if(res.data.meta.status!==200) return this.$message.error('获取父级分类失败')
      this.addCateCascader=res.data.data
      this.addCateDialogVisible=true
      console.log(this.addCateCascader);
    },
    closeAddCate(){
      this.$refs.add_cate_form.resetFields()
      this.addCateForm.cat_pid=0
      this.addCateForm.cat_level=0
      this.selectedIds=[]
    },
      confirmAddCate(){
       this.$refs.add_cate_form.validate(async valid=>{
         if(valid){
            //  debugger
            if(this.selectedIds.length==0){
              this.addCateForm.cat_pid=0
            }else{
              this.addCateForm.cat_pid=this.selectedIds[this.selectedIds.length-1]
            }
            this.addCateForm.cat_level=this.selectedIds.length
            const res = await this.$http.post('categories',this.addCateForm)
            if(res.data.meta.status!==201) return this.$message.error('创建失败')
            this.initGoodsList()
            this.$message.success('创建成功')
            // console.log(this.addCateForm);
            // console.log(this.addCateForm.cat_name);
            this.addCateDialogVisible=false
         }
       })
      
    },
    editCate(row){
      console.log(row);
      this.editcate.cat_id=row.cat_id
      this.editcate.editcate_newname=row.cat_name
      this.editCateDialogVisible=true
    },
    async confirmEditCate(){
      const res=await this.$http.put('categories/'+this.editcate.cat_id,{
        cat_name:this.editcate.editcate_newname
      })
      console.log(res);
      if(res.data.meta.status!==200) return this,$message.error('更新失败')
      this.$message.success('更新成功')
      this.initGoodsList()
      this.editCateDialogVisible=false
    },
    async delCate(cate_id){
      const flag =await this.$confirm('此操作会永久删除该分类,是否确定','提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).catch(err=>{
        return  err
      })
      if(flag=="confirm"){
        console.log(cate_id);
        const res=await this.$http.delete('categories/'+cate_id)
        console.log(res);
        if(res.data.meta.status!==200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.initGoodsList()
      }
    }
  }
}
</script>
<style>
.el-cascader{
      width: 100%;
    }
</style>
<style lang="less" scoped>
.goods_list{
  .el-card{
    margin-top: 20px;
    .el-button{
      margin-bottom: 20px;
    }
    .goods_button{
      margin-bottom: 0;
      margin-left: 10px;
    }
    .el-pagination{
      margin-top: 20px;
    }
    
  }
}
</style>